#ifndef _SERVER_H_
#define _SERVER_H_

#include "ecmfs_cmn.h"
#include "configfile.h"

#define MASTER_MSG_QNAME "/master_mq"
#define EDGE_MSG_QNAME "/edge_mq"
#define CORE_MSG_QNAME "/core_mq"
#define EXIT_FAILURE 1

typedef struct server_thread_info_ {
     pthread_t                 thread_id;
     pid_t                     pid;
     pid_t                     tid;
} server_thread_info_t;



#define MAXMSG  (sizeof(ecmfs_message_t) + 4096)
#define MAGIC_NUM 19977991


#define MAX_PUB_KEY_LEN 2048




typedef struct ecmfs_client_master_response_ {
    server_state_t state;
    server_resp_t resp;
    int more;
    int msg_len;
    char msg[1]; 
} ecmfs_server_response_t;



int start_listening(config_file_t *cfile,pthread_t  *thread_id);
int prepare_socket_connection(void);
int receive_message(int filedes, config_file_t *cfile);


int create_master_thread(void);
void *master_thread (void *arg);
bool_ecmfs master_msg_send (unsigned char *buffer, int buff_size, int sd);
bool_ecmfs master_handle_client_message(ecmfs_message_t *msg, int *more);
bool_ecmfs master_handle_edge_message(ecmfs_message_t *msg, int *more);
bool_ecmfs master_handle_core_message(ecmfs_message_t *msg, int *more);
bool_ecmfs master_handle_master_message(ecmfs_message_t *msg, int *more);

int create_edge_thread(void);
void *edge_thread (void *arg);
bool_ecmfs edge_msg_send (unsigned char *buffer, int buff_size, int sd);
bool_ecmfs edge_handle_client_message(ecmfs_message_t *msg, int *more);
bool_ecmfs edge_handle_edge_message(ecmfs_message_t *msg, int *more);
bool_ecmfs edge_handle_core_message(ecmfs_message_t *msg, int *more);
bool_ecmfs edge_handle_master_message(ecmfs_message_t *msg, int *more);
void edge_handle_message(unsigned char *buffer, int bufsize, int *more);

int create_core_thread(void);
void *core_thread (void *arg);
bool_ecmfs core_msg_send (unsigned char *buffer, int buff_size, int sd);
bool_ecmfs core_handle_client_message(ecmfs_message_t *msg, int *more);
bool_ecmfs core_handle_edge_message(ecmfs_message_t *msg, int *more);
bool_ecmfs core_handle_core_message(ecmfs_message_t *msg, int *more);
bool_ecmfs core_handle_master_message(ecmfs_message_t *msg, int *more);


#endif
